import { useState, useEffect } from "react";

function Test() {
  useEffect(() => {
    const timer = setInterval(() => {
      console.log(1);
    }, 2000)
    // useEffect 消除一些方法的副作用时，必须是return 一个箭头函数
    return () => {
      clearInterval(timer)
    }
  })
  return (
    <div>
      this is test
    </div>
  )
}

function App () {
  const [flag, setFlag] = useState(true)
  return (
    <div>
      { flag ? <Test></Test> : "" }
      <button onClick={() => setFlag(!flag)}>switch</button>
    </div>
  )
}

export default App;
